package org.smartercontext.local.servlets;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.smartercontext.contextmger.DTO.PaymentMethodDTO;
import org.smartercontext.contextmger.contextsphere.ContextManagerController;
import org.smartercontext.contextmger.utilities.GetUserPaymentMethod;

import com.hp.hpl.jena.rdf.model.Model;

/**
 * Servlet implementation class DeletePaymentMethodServlet
 */
@WebServlet("/DeletePaymentMethodServlet")
public class DeletePaymentMethodServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public DeletePaymentMethodServlet() {
        super();
        // TODO Auto-generated constructor stub
    }
    
    protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
    	if(request.getQueryString()!=null){
    		HttpSession session = request.getSession();
    		
    		String username = session.getAttribute("username").toString();
    		
    		if(username==null){
    			response.sendRedirect("login.jsp?Err=3");
    		}
    		
    		String paymentM = request.getParameter("paymentM");
    		GetUserPaymentMethod gupm = new GetUserPaymentMethod();
    		
    		PaymentMethodDTO pmdto = new PaymentMethodDTO();
    		pmdto.setName(paymentM);
    		
    		try {
    			ContextManagerController.contextManagerController();
				Model usermodel=ContextManagerController.getUsermodel(username);
				Model usermodelU = gupm.deletePaymentMethod(usermodel, username, pmdto);
				ContextManagerController.integrateContext(usermodelU, username);
				response.sendRedirect("manageContext_PaymentM.jsp");
				
			} catch (Exception e) {
				response.sendRedirect("manageContext_PaymentM.jsp?Error=3;");
			}
    		
    	}
    	
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		processRequest(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		processRequest(request, response);
	}

}
